<?php
class Pager{

	/**
	* Find first element of curent page
	* @param $limit int total element show on one page
	* @return Retunr first element 
	*/

	function findStart($limit) {
		if (!isset($_GET['page']) || ($_GET['page']=="1"))
		{
			$start=0;
			$_GET['page']==1;
		}
		else {
			$start=($_GET['page']-1)*$limit;
		}
		return $start;
	}

	/**
	* Tính tổng số trang cần dùng
	* @param $count  int  total element want show
	* @param $limit int total element show on one page
	* @return Return total page
	**/
	function findPages($count,$limit) {
		$pages=(($count % $limit)==0) ? $count/$limit:floor($count/$limit)+1;
		return $pages;
	}

	/**
	* 
	*/

	function pageList($curpage,$pages) {
		$page_list="";
		if(($curpage!=1) && ($curpage)) {
			$page_list.="Trang đầu";
		}
		if(($curpage-1)>0) {
			$page_list.="<";
		}
	/*in danh sach cac trang va lam cho link trang hien tai dam hon va mat gach chan*/
		for($i=1; $i<=$pages; $i++){
		    if($i==$curpage){
		  	  $page_list.="".$i."";
		    }
		    else
		    {
		  	  $page_list.="".$i."";
		    }
		    $page_list.=" ";
		}
	/*in lick cua trang tiep theo*/
		if(($curpage+1)<= $pages)
		{
			$page_list.=">";
		}
		if (($curpage!=$pages) &&($pages!=0)) {
			$page_list.="?page=".$pages."\\" title=Trang cuối\\">Trang cuối";
		}
		$page_list.="n";
		return $page_list;
	}
	function nextPrev($curpage,$pages) {
		$next_prev="";
		if (($curpage=1)<=0){
			$next_prev.="Trở về trang trước";
		}
		else
		{
			$next_prev.="Về trang trước";
		}
		$next_prev.="|";
		if(($curpage+1)>$pages){
			$next_prev.="Ð?n trang sau";
		}
		else
		{
			$next_prev.="Đến trang sau";
		}
		return  $next_prev;
	}
}

//gọi và sử dụng
include("config.php");
$p=new Pager;
$limit=10;
$start=$p->findStart($limit);
$count=mysql_num_rows(mysql_query("select * from sinh_vien"));
$pages=$p->findPages($count,$limit);
$result=mysql_query("select * from sinh_vien limit ".$start.",".$limit);
while($row=mysql_fetch_object($result)){
$ten_sinh_vien=$row->TEN_SINH_VIEN;
echo "$ten_sinh_vien
";
}
$page_list=$p->pageList($_GET['page'],$pages);
echo "";
echo $page_list;
echo "
";

?>  